package com.budwk.app.fetchdata.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.fetchdata.models.FetchData_volunteerpopulation;
import com.budwk.app.fetchdata.services.FetchdataVolunteerpopulationService;
import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.Static;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@IocBean
@At("/platform/fetchdata/volunteer")
@ApiDefinition(tag = "志愿者数据表")
@Slf4j
public class FetchdataVolunteerpopulationController{
    @Inject
    private FetchdataVolunteerpopulationService wxVolunteerService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询志愿者数据表")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = FetchData_volunteerpopulation.class
    )
    @ApiResponses(
    implementation = FetchData_volunteerpopulation.class
    )
    @SaCheckPermission("fetchdata.volunteer")
    public Result<?> list(@Param("..")FetchData_volunteerpopulation bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getName() !=null && Strings.isNotBlank(bean.getName().toString())) {
            cnd.and("name","like","%"+bean.getName()+"%");
        }
        
        if (bean.getSexual() !=null && Strings.isNotBlank(bean.getSexual().toString())) {
            cnd.and("sexual","like","%"+bean.getSexual()+"%");
        }
        
        if (bean.getPhone() !=null && Strings.isNotBlank(bean.getPhone().toString())) {
            cnd.and("phone","like","%"+bean.getPhone()+"%");
        }
        
        if (bean.getVxprovinces() !=null && Strings.isNotBlank(bean.getVxprovinces().toString())) {
            cnd.and("vxprovinces","like","%"+bean.getVxprovinces()+"%");
        }
        
        if (bean.getVxcities() !=null && Strings.isNotBlank(bean.getVxcities().toString())) {
            cnd.and("vxcities","like","%"+bean.getVxcities()+"%");
        }
        
        if (bean.getYears() !=null && Strings.isNotBlank(bean.getYears().toString())) {
            cnd.and("years","like","%"+bean.getYears().substring(0,4)+"%");
        }
        
        if (bean.getProvinces() !=null && Strings.isNotBlank(bean.getProvinces().toString())) {
            cnd.and("provinces","like","%"+bean.getProvinces()+"%");
        }
        
        if (bean.getCities() !=null && Strings.isNotBlank(bean.getCities().toString())) {
            cnd.and("cities","like","%"+bean.getCities()+"%");
        }
        
        if (bean.getDistricts() !=null && Strings.isNotBlank(bean.getDistricts().toString())) {
            cnd.and("districts","like","%"+bean.getDistricts()+"%");
        }
        
        if (bean.getTownships() !=null && Strings.isNotBlank(bean.getTownships().toString())) {
            cnd.and("townships","like","%"+bean.getTownships()+"%");
        }
        
        if (bean.getServicetype() !=null && Strings.isNotBlank(bean.getServicetype().toString())) {
            cnd.and("servicetype","like","%"+bean.getServicetype()+"%");
        }
        
        if (bean.getServicetime() !=null && Strings.isNotBlank(bean.getServicetime().toString())) {
            cnd.and("servicetime","like","%"+bean.getServicetime()+"%");
        }
        
        if (bean.getRegistrationtime() !=null && Strings.isNotBlank(bean.getRegistrationtime().toString())) {

           Instant instant = bean.getRegistrationtime().toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            /*Date date=new Date();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String a=simpleDateFormat.format( bean.getRegistrationtime());*/


            String startdateStr = localDateTime.format(fmt);
            String enddateStr = localDateTime.plusDays(1).format(fmt);
            cnd.and("registrationtime","between",new Object[]{startdateStr,enddateStr});

        }
        
        if (bean.getCreatetime() !=null && Strings.isNotBlank(bean.getCreatetime().toString())) {
            cnd.and("createtime","like","%"+bean.getCreatetime()+"%");
        }
        
        return Result.data(wxVolunteerService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增志愿者数据表")
    @ApiFormParams(
        implementation = FetchData_volunteerpopulation.class
    )
    @ApiResponses
    @SLog(tag = "新增志愿者数据表", value = "${args[0].id}")
    @SaCheckPermission("fetchdata.volunteer.create")
    public Result<?> create(@Param("..")FetchData_volunteerpopulation wxVolunteer, HttpServletRequest req) {
        wxVolunteer.setCreatedBy(SecurityUtil.getUserId());
        String str=wxVolunteer.getServicetime();
        if (str.substring(str.length()-1).equals(",")){
            str=str.substring(0,str.length()-1);
            wxVolunteer.setServicetime(str);
        }
        wxVolunteerService.insert(wxVolunteer);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改志愿者数据表")
    @ApiFormParams(
        implementation = FetchData_volunteerpopulation.class
    )
    @ApiResponses
    @SLog(tag = "修改志愿者数据表", value = "${args[0].id}")
    @SaCheckPermission("fetchdata.volunteer.update")
    public Result<?> update(@Param("..")FetchData_volunteerpopulation wxVolunteer, HttpServletRequest req) {
        wxVolunteer.setUpdatedBy(SecurityUtil.getUserId());
        String str=wxVolunteer.getServicetime();
        if (str.substring(str.length()-1).equals(",")){
            str=str.substring(0,str.length()-1);
            wxVolunteer.setServicetime(str);
        }
        wxVolunteerService.updateIgnoreNull(wxVolunteer);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取志愿者数据表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("fetchdata.volunteer")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        FetchData_volunteerpopulation wxVolunteer = wxVolunteerService.fetch(id);
        if (wxVolunteer == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxVolunteer);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除志愿者数据表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除志愿者数据表", value = "${args[0]}")
    @SaCheckPermission("fetchdata.volunteer.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        FetchData_volunteerpopulation wxVolunteer = wxVolunteerService.fetch(id);
        if (wxVolunteer == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxVolunteerService.delete(id);
        req.setAttribute("_slog_msg", wxVolunteer.getId());
        return Result.success();
    }
}
